﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PM.Controls;
using HealthManagement.Data.Service;
using HealthManagement.Data.Model;
using PM.Controls.Common;

namespace HealthManagement.View
{
    /// <summary>
    /// DoctorForm.xaml 的交互逻辑
    /// </summary>
    public partial class DoctorForm : PMWindow
    {
        DoctorUser user = new DoctorUser();
        public event Action onSaveHandler;

        public DoctorForm()
        {
            InitializeComponent();
            this.DataContext = user;
        }

        public DoctorForm(string userid) 
        {
            InitializeComponent();
           
            UserService ser = new UserService();
            user = ser.LoadDoctorUserList(c=>c.userId == userid).Single();
            this.DataContext = user;
        }


        private void save_Click(object sender, RoutedEventArgs e)
        {

            UserService ser = new UserService();



            if (string.IsNullOrEmpty(user.userId))
            {
                user.userId = Guid.NewGuid().ToString().Substring(0, 20);
                user.createDate = DateTime.Now;

                int idcard = ser.GetDoctorUserMaxIdCard();
                user.IdCard = (idcard + 1).ToString("0000");

                ser.Add(user);
                Tools.ShowMessage(true, string.Format("新增成功，医师编号为：{0}", user.IdCard));
            }
            else
            {
                ser.Update(user, "userId", "userId");
            }

            this.Close();
            if (onSaveHandler != null)
                onSaveHandler();
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.CloseWindow();
        }
    }
}
